// console.log(getApp().globalData)
var axios = require("../util/request.js")
var app = getApp()
// console.log(app)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dayStyle: [
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' },
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' }
    ],
    token: '',
    newday: '',
    startTime: '',
    endTime: '',
    type: '0',
    data: '',
    hiddenmodalput: false,
    hiddenmodalput1: false,
    time: '',
    amount: '',
    num: '',
    tempimg: '',
    tempimg1: false,
    base6: '',
    uptype: '',
    red: '',
    start: '',
    tabBar: '',
    id: '',
    select: false,
    tihuoWay: '',
    array: '',
    array1: '',
    index: '',
    shoptype: '2',
    shop_id: '',
    shopname1: "",
    listData: [],
    num_num: 0,
    num: '',
    alerttype: ''
  },
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  // 门店确定
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log(this.data.array[e.detail.value])
    var shopname = this.data.array[e.detail.value]
    // this.setData({

    // })
    var dataarray1 = this.data.array1
    dataarray1.forEach((res) => {
      if (res.shop_name == shopname) {
        // console.log(res)
        this.setData({
          shop_id: res.shop_id

        })
      }
    })
    if (shopname == '所有') {
      var data = {
        "start_day": this.data.startTime,
        "end_day": this.data.startTime,
        'shop_id': '',
        
        "retail_form_id": '1',
        "num": 100,
        "page": 1
      }
      this.setData({
        shoptype: '1',

      })
    } else {
      var data = {
        "start_day": this.data.startTime,
        "end_day": this.data.endTime,
        'shop_id': this.data.shop_id,
        
        "retail_form_id": '1',
        "num": 100,
        "page": 1
      }
      this.setData({
        shoptype: '2',
        index: e.detail.value,
        data: '',


      })
    }

    this.dateinterface(data)

  },
  mySelect(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      tihuoWay: name,
      select: false
    })
  },
  //给点击的日期设置一个背景颜色
  bindDateChange1: function (e) {

    console.log('aaaa')
    if (this.data.shop_id == '' || this.data.shop_id === null || this.data.shop_id===undefined){
      wx.showToast({
        title: '请填写门店',
        icon: 'none'
      })
      setTimeout(() => {
        wx.hideLoading()
      }, 800)
    }else{
      var data = {
        "start_day": e.detail.value,
        "end_day": this.data.endTime,
        'shop_id': this.data.shop_id,
        "retail_form_id": '1',
        "num": 100,
        "page": 1
      }
      if (e.detail.value < this.data.endTime) {
        this.dateinterface(data)
      } else {
        wx.showToast({
          title: '开始时间不能大于结束时间',
          icon: 'none'
        })
      }

     
    }
    this.setData({
      startTime: e.detail.value,
      type: '1'
    })
   
  },

  bindDateChange2: function (e) {

    this.setData({
      endTime: e.detail.value,
      type: '2'
    })
    if (this.data.shop_id == '' || this.data.shop_id === null || this.data.shop_id === undefined) {
      wx.showToast({
        title: '请填写门店',
        icon: 'none'
      })
    
    } else {
    var data = {
      "end_day": e.detail.value,
      "start_day": this.data.startTime,
      'shop_id': this.data.shop_id,
      
      "retail_form_id": '1',
      "num": 100,
      "page": 1
    }
    if (e.detail.value > this.data.startTime) {
      this.dateinterface(data)
    } else {
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
    }
    }
    // this.dateinterface(data)
  },
  bindDateChange3: function (e) {

    var data = {
      "start_day": e.detail.value,
      "end_day": e.detail.value,
      "shop_id": '',
      
      "retail_form_id": '1',
      "num": 100,
      "page": 1
    }
    if (e.detail.value < this.data.endTime) {
      this.dateinterface(data)
    } else {
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
    }

    this.setData({
      startTime: e.detail.value,
      type: '1'
    })
  },

  //  请求日期接口
  dateinterface(data) {
    axios.post('mall_manager/count_day_ticket', data, this.shuffleSuc);
  },
  //  请求门店
  shoplist(data) {
    axios.getData('mall_manager/shop_list', this.shoplist1);
  },
  shoplist1(res) {
    // console.log(res.data.data.items)
    var shoulist = res.data.data.items
    var list = []
    shoulist.forEach((res1) => {
      //  console.log(res1.shop_name)
      list.push(res1.shop_name)
    })
    this.setData({
      array: list,
      array1: shoulist
    })
  },
  shuffleSuc(res) {
    console.log(res)
  },
  // 得到当前日期
  double(num) {//让单数变成双数
    if (num < 10) {
      return '0' + num;//单数前面添加0
    } else {
      return num;
    }
  },
  time() {
    var that = this;
    var date = new Date();//创建一个日期对象
    var year = date.getFullYear()//获取4位年份
    var month = date.getMonth() + 1;//+1
    var day = date.getDate();
    var str = '';
    var hour = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    str = year + '-' + this.double(month) + '-' + this.double(day)
    this.setData({
      newday: str
    })

    this.setData({
      endTime: str,
      startTime: str
    })
    var data = {
      "end_day": str,
      "start_day": str,

    }
    setTimeout(function () {
      // that.dateinterface(data)
      that.shoplist()
    }, 100)
    // 最大选择90天数据
    var date1 = new Date();
    var date2 = new Date(date1);
    date2.setDate(date1.getDate() - 90);
    var state = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate()
    this.setData({
      start: state

    })
    // console.log(state);
  },


  // 退回
  cancelM: function (e) {

    var json = {
      'data_day': this.data.tihuoWay.day,
      'shop_id': this.data.tihuoWay.shop_id,
      'upload_status': 2,

    }
    axios.post('mall_manager/mall_verify_upload_data', JSON.stringify(json), this.shuffleSuc1);
    this.setData({
      hiddenmodalput: false,
    })
  },
  cancelM1: function (e) {
    this.setData({
      hiddenmodalput: false,
    })
  },
  // 通过
  confirmM: function (e) {
    // console.log(this.data.tihuoWay)
    // console.log(e)
    // console.log(e.currentTarget)





    this.setData({
      hiddenmodalput: false,
    })

    // console.log("姓名：" + this.data.name + "  电话：" + this.data.phoneNum);
  },
  // 请求上报成功
  shuffleSuc1(res) {
    console.log(res)
    if (res.data.code == 'E000000') {
      var data = {
        "end_day": this.data.endTime,
        "start_day": this.data.startTime,
        "shop_id": this.data.shop_id,
       
        "retail_form_id": '1',
        "page": 1,
        "num": 100,
      }
      this.dateinterface(data)
    } else {
      wx.showToast({
        title: res.desc,
        icon: 'none'
      })
    }

  },
  // 下拉刷新
  onPullDownRefresh: function (a) {
    console.log('1')
    var data = {
      "end_day": this.data.endTime,
      "start_day": this.data.startTime,
      "shop_id": this.data.shop_id,
      "num": 100,
    
      "retail_form_id": '1',
      "page": 1
    }
    this.dateinterface(data)
    wx.stopPullDownRefresh()
  },
  // 上拉加载
  onReachBottom: function (e) {

    // if (Math.ceil(this.data.num) > this.data.num_num) {
    //   wx.showLoading({
    //     title: '数据加载中',
    //   })
    //   var data = {
    //     "end_day": this.data.endTime,
    //     "start_day": this.data.startTime,
    //     "shop_id": this.data.shop_id,
    //     "num": 100,
    
    //     "retail_form_id": '1',
    //     "page": this.data.num_num
    //   }
    //   this.dateinterface(data)

    // } else {
    //   wx.showLoading({
    //     title: '无更多数据',
    //   })
    // }
    // setTimeout(function () {
    //   wx.hideLoading();
    // }, 200)
    // setTimeout(function () {
    //   wx.pageScrollTo({
    //     scrollTop: 0
    //   })
    // }, 1000)

  },

  // 请求修改成功
  shuffleSuc2(res) {
    var data = {
      "end_day": this.data.endTime,
      "start_day": this.data.startTime,
      "shop_id": '',
      "num": 100,
 
      "retail_form_id": '1',
      "page": 1
    }
    this.dateinterface(data)
  },
  amount(a) {
    this.setData({
      amount: a.detail.value
    })

  },
  num(a) {
    this.setData({
      num: a.detail.value
    })

  },
  // 修改
  addrevamp(value) {
    console.log(value)
    this.setData({
      uptype: '2',
      tihuoWay: value.currentTarget.dataset.id,
      time: value.currentTarget.dataset.id.day,
      amount: value.currentTarget.dataset.id.upload_amount,
      num: value.currentTarget.dataset.id.upload_number,
      hiddenmodalput1: true,
      tempimg1: false,
      amount: '',
      num: '',
      base6: ''
    })
  },
  confirmM1() {
    this.setData({
      hiddenmodalput1: false
    })
  },
  // 查看看
  updata(value) {

    axios.post('mall_manager/show_ticket_content', { 'ticket_id_original': value.currentTarget.dataset.id }, this.shuffleSucs);
    this.setData({
      hiddenmodalput: true,

    })

  },
  shuffleSucs: function (res) {
    this.setData({
      alerttype: res.data.data
    })
    console.log(res.data.data)
  },
  onLoad: function (id) {

    // 请求门店

    this.time()
    app.editTabBar()
    // console.log(app)
    this.setData({
      tabBar: app,
      id: id.id
    })
    var data = {
      "end_day": this.data.endTime,
      "start_day": this.data.startTime,
      "shop_id": '',
      "num": 100,
     
      "retail_form_id": '1',
      "page": 1
    }
    this.dateinterface(data)


  },
  shuffleSuc(res) {
    var that = this;
    if (res.data.code == 'E000000') {
      const data_items = res.data.data.items
     
      if (res.data.data.items.length>"0"){
        
       
      }else{
        wx.showToast({
          title: '请填写门店',
          icon: 'none'
        })
      }
      
      this.setData({
        listData: data_items.reverse(),
        num: res.data.data.num / 100,
        num_num: that.data.num_num += 1
      })
    } else {
      wx.showLoading({
        title: '暂无数据',
        mask: true
      })
      setTimeout(() => {
        wx.hideLoading()
      }, 500)
    }
    // console.log(res)
  }
})
